package com.bdqn.t320.ch12.controller;

import com.bdqn.t320.ch12.entity.Role;
import com.bdqn.t320.ch12.entity.User;
import com.bdqn.t320.ch12.entity.UserInfo;
import com.bdqn.t320.ch12.service.RoleService;
import com.bdqn.t320.ch12.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;


    @RequestMapping(value = "/list")
    public String list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "usrName", required = false, defaultValue = "") String usrName,
                       @RequestParam(value = "roleId", required = false, defaultValue = "0") Long roleId,
                       Model model) {

/*        Map<String, String> map = new HashMap<>();
        map.put("usrName", usrName);
        map.put("roleId", roleId.toString());
        Sort sort = new Sort(Sort.Direction.ASC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
        Page<User> page = userService.findPageByMap(map, pageable);

        Sort roleSort = new Sort(Sort.Direction.ASC, "roleId");
        List<Role> roles = roleService.getRoleListService(roleSort);

        model.addAttribute("userPager", page);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        model.addAttribute("roles", roles);*/

        List<Map<String, Object>> userList = userService.getUserAllBiz();
        model.addAttribute("userList", userList);
        return "user/list";
    }

    @ResponseBody
    @RequestMapping(value = "/rolelist")
    public Object roleList() {
        Sort roleSort = new Sort(Sort.Direction.ASC, "roleId");
        return roleService.getRoleListService(roleSort);
    }

    @RequestMapping(value = "/add")
    public Object add() {
        return "user/add";
    }

    @ResponseBody
    @RequestMapping(value = "/getrolelist")
    public Object getRoleList(Model model) {
        Sort roleSort = new Sort(Sort.Direction.ASC, "roleId");
        return roleService.getRoleListService(roleSort);
    }

}
